

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/page/favicon.jpg">
  <link rel="icon" type="image/png" href="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/page/favicon.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="这是我的小笔记本,主要要用记录学习过程,当然也会记录一些随心笔记.">
  <meta name="author" content="筏执">
  <meta name="keywords" content="">
  <title>关于C++中fstream使用(ios::in|ios::out)在当文件不存在时不创建文件而是打开错误 - 筏执的小笔记本</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.5.3/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.1.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.5","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"copy_btn":true,"image_zoom":{"enable":true},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":true,"baidu":"6c1436d3df29ec7c0cda293342e60e93","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":1279445150,"leancloud":{"app_id":null,"app_key":null,"server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>筏执的笔记本</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                网站
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/page/HomePage.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="关于C++中fstream使用(ios::in|ios::out)在当文件不存在时不创建文件而是打开错误">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-20 00:24" pubdate>
        2020年12月20日 凌晨
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      74
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">关于C++中fstream使用(ios::in|ios::out)在当文件不存在时不创建文件而是打开错误</h1>
            
            <div class="markdown-body">
              <blockquote>
<p>起因</p>
</blockquote>
<p>&emsp;&emsp;今天晚上本来闲聊的时候,突然滴滴的一声,我的小眼睛一瞟,哦~,原来是学弟在群里面问问题,本着学长的心态,依然义无反顾的点击进去,准备好好的回答一下学弟的小问题,结果,自己学艺不精,被难倒了.于是,就有了这一次记录.</p>
<blockquote>
<p>学弟的问题</p>
</blockquote>
<p>&emsp;&emsp;<strong>为什么fstream在使用ios::in|ios::out|ios::binary打开文件(文件不存在)时,不会创建文件,且打开文件失败.</strong></p>
<p><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/option1.png" srcset="/img/loading.gif" alt="图1"></p>
<p><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/option2.png" srcset="/img/loading.gif" alt="图2"></p>
<blockquote>
<p>解决问题</p>
</blockquote>
<p>&emsp;&emsp;起初,本以为是因为ios::in的问题,猜测可能是先使用了ios::in,结果文件不存在,导致文件打开失败,然后直接返回,而忽略了ios::out的文件不存在则创建文件.然后,总感觉哪里不对,于是,我决定去源文件中去看看到底是为什么导致了文件无法创建.</p>
<p>&emsp;&emsp;首先是去头文件中查看以下,以下是fstream的部分代码(只挑选了一些比较重要的代码,外加删除了一点点.):</p>
<pre><code class="hljs sqf"><span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> _GLIBCXX_FSTREAM</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> _GLIBCXX_FSTREAM 1</span>

<span class="hljs-meta">#pragma GCC system_header</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;istream&gt;             //输入流头文件</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;ostream&gt;             //输出流头文件</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/codecvt.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;             // For BUFSIZ</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/basic_file.h&gt;  // For __basic_file, __c_lock</span></span>
<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;             // For std::string overloads.</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>


<span class="hljs-comment">//basic_filebuf类,继承的basic_streambuf类</span>
<span class="hljs-comment">// [27.8.1.1] template class basic_filebuf</span>
<span class="hljs-comment">/**</span>
<span class="hljs-comment"> *  @brief  The actual work of input and output (for files).</span>
<span class="hljs-comment"> *  @ingroup io</span>
<span class="hljs-comment"> *</span>
<span class="hljs-comment"> *  @tparam _CharT  Type of character stream.</span>
<span class="hljs-comment"> *  @tparam _Traits  Traits for character type, defaults to</span>
<span class="hljs-comment"> *                   char_traits&lt;_CharT&gt;.</span>
<span class="hljs-comment"> *</span>
<span class="hljs-comment"> *  This class associates both its input and output sequence with an</span>
<span class="hljs-comment"> *  external disk file, and maintains a joint file position for both</span>
<span class="hljs-comment"> *  sequences.  Many of its semantics are described in terms of similar</span>
<span class="hljs-comment"> *  behavior in the Standard C Library&#x27;s @c FILE streams.</span>
<span class="hljs-comment"> *</span>
<span class="hljs-comment"> *  Requirements on traits_type, specific to this class:</span>
<span class="hljs-comment"> *  - traits_type::pos_type must be fpos&lt;traits_type::state_type&gt;</span>
<span class="hljs-comment"> *  - traits_type::off_type must be streamoff</span>
<span class="hljs-comment"> *  - traits_type::state_type must be Assignable and DefaultConstructible,</span>
<span class="hljs-comment"> *  - traits_type::state_type() must be the initial state for codecvt.</span>
<span class="hljs-comment"> */</span>
template&lt;<span class="hljs-built_in">typename</span> <span class="hljs-variable">_CharT</span>, <span class="hljs-built_in">typename</span> <span class="hljs-variable">_Traits</span>&gt;
  class basic_filebuf : public basic_streambuf&lt;<span class="hljs-variable">_CharT</span>, <span class="hljs-variable">_Traits</span>&gt;
  &#123;
  public:
    <span class="hljs-comment">// Types:</span>
    typedef <span class="hljs-variable">_CharT</span>                     	        char_type;
    typedef <span class="hljs-variable">_Traits</span>                    	        traits_type;
    typedef <span class="hljs-built_in">typename</span> traits_type::int_type 		int_type;
    typedef <span class="hljs-built_in">typename</span> traits_type::pos_type 		pos_type;
    typedef <span class="hljs-built_in">typename</span> traits_type::off_type 		off_type;

    typedef basic_streambuf&lt;char_type, traits_type&gt;  	<span class="hljs-variable">__streambuf_type</span>;
    typedef basic_filebuf&lt;char_type, traits_type&gt;     <span class="hljs-variable">__filebuf_type</span>;
    typedef <span class="hljs-variable">__basic_file</span>&lt;char&gt;		        <span class="hljs-variable">__file_type</span>;
    typedef <span class="hljs-built_in">typename</span> traits_type::state_type          <span class="hljs-variable">__state_type</span>;
    typedef codecvt&lt;char_type, char, <span class="hljs-variable">__state_type</span>&gt;    <span class="hljs-variable">__codecvt_type</span>;

    friend class ios_base; <span class="hljs-comment">// For sync_with_stdio.</span>

  protected:
    <span class="hljs-comment">// Data Members:</span>
    <span class="hljs-comment">// MT lock inherited from libio or other low-level io library.</span>
    <span class="hljs-variable">__c_lock</span>          	<span class="hljs-variable">_M_lock</span>;

    <span class="hljs-comment">// External buffer.</span>
    <span class="hljs-variable">__file_type</span> 		<span class="hljs-variable">_M_file</span>;

    <span class="hljs-comment">/// Place to stash in || out || in | out settings for current filebuf.</span>
    ios_base::openmode 	<span class="hljs-variable">_M_mode</span>;

    <span class="hljs-comment">// Beginning state type for codecvt.</span>
    <span class="hljs-variable">__state_type</span> 		<span class="hljs-variable">_M_state_beg</span>;

    <span class="hljs-comment">// During output, the state that corresponds to pptr(),</span>
    <span class="hljs-comment">// during input, the state that corresponds to egptr() and</span>
    <span class="hljs-comment">// _M_ext_next.</span>
    <span class="hljs-variable">__state_type</span>		<span class="hljs-variable">_M_state_cur</span>;

    <span class="hljs-comment">// Not used for output. During input, the state that corresponds</span>
    <span class="hljs-comment">// to eback() and _M_ext_buf.</span>
    <span class="hljs-variable">__state_type</span>		<span class="hljs-variable">_M_state_last</span>;

    <span class="hljs-comment">/// Pointer to the beginning of internal buffer.</span>
    char_type*		<span class="hljs-variable">_M_buf</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Actual size of internal buffer. This number is equal to the size</span>
<span class="hljs-comment">     *  of the put area + 1 position, reserved for the overflow char of</span>
<span class="hljs-comment">     *  a full area.</span>
<span class="hljs-comment">     */</span>
    size_t			<span class="hljs-variable">_M_buf_size</span>;

    <span class="hljs-comment">// Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.</span>
    bool			<span class="hljs-variable">_M_buf_allocated</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  _M_reading == false &amp;&amp; _M_writing == false for @b uncommitted mode;</span>
<span class="hljs-comment">     *  _M_reading == true for @b read mode;</span>
<span class="hljs-comment">     *  _M_writing == true for @b write mode;</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  NB: _M_reading == true &amp;&amp; _M_writing == true is unused.</span>
<span class="hljs-comment">     */</span>
    bool                      <span class="hljs-variable">_M_reading</span>;
    bool                      <span class="hljs-variable">_M_writing</span>;

    <span class="hljs-comment">//@&#123;</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Necessary bits for putback buffer management.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  @note pbacks of over one character are not currently supported.</span>
<span class="hljs-comment">     */</span>
    char_type			<span class="hljs-variable">_M_pback</span>;
    char_type*		<span class="hljs-variable">_M_pback_cur_save</span>;
    char_type*		<span class="hljs-variable">_M_pback_end_save</span>;
    bool			<span class="hljs-variable">_M_pback_init</span>;
    <span class="hljs-comment">//@&#125;</span>

    <span class="hljs-comment">// Cached codecvt facet.</span>
    const <span class="hljs-variable">__codecvt_type</span>* 	<span class="hljs-variable">_M_codecvt</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Buffer for external characters. Used for input when</span>
<span class="hljs-comment">     *  codecvt::always_noconv() == false. When valid, this corresponds</span>
<span class="hljs-comment">     *  to eback().</span>
<span class="hljs-comment">     */</span>
    char*			<span class="hljs-variable">_M_ext_buf</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Size of buffer held by _M_ext_buf.</span>
<span class="hljs-comment">     */</span>
    streamsize		<span class="hljs-variable">_M_ext_buf_size</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Pointers into the buffer held by _M_ext_buf that delimit a</span>
<span class="hljs-comment">     *  subsequence of bytes that have been read but not yet converted.</span>
<span class="hljs-comment">     *  When valid, _M_ext_next corresponds to egptr().</span>
<span class="hljs-comment">     */</span>
    const char*		<span class="hljs-variable">_M_ext_next</span>;
    char*			<span class="hljs-variable">_M_ext_end</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Initializes pback buffers, and moves normal buffers to safety.</span>
<span class="hljs-comment">     *  Assumptions:</span>
<span class="hljs-comment">     *  _M_in_cur has already been moved back</span>
<span class="hljs-comment">     */</span>
    void
    <span class="hljs-variable">_M_create_pback</span>()
    &#123;
<span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_pback_init</span>)
  &#123;
    <span class="hljs-variable">_M_pback_cur_save</span> = this-&gt;gptr();
    <span class="hljs-variable">_M_pback_end_save</span> = this-&gt;egptr();
    this-&gt;setg(&amp;<span class="hljs-variable">_M_pback</span>, &amp;<span class="hljs-variable">_M_pback</span>, &amp;<span class="hljs-variable">_M_pback</span> + <span class="hljs-number">1</span>);
    <span class="hljs-variable">_M_pback_init</span> = <span class="hljs-literal">true</span>;
  &#125;
    &#125;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Deactivates pback buffer contents, and restores normal buffer.</span>
<span class="hljs-comment">     *  Assumptions:</span>
<span class="hljs-comment">     *  The pback buffer has only moved forward.</span>
<span class="hljs-comment">     */</span>
    void
    <span class="hljs-variable">_M_destroy_pback</span>() <span class="hljs-keyword">throw</span>()
    &#123;
<span class="hljs-keyword">if</span> (<span class="hljs-variable">_M_pback_init</span>)
  &#123;
    <span class="hljs-comment">// Length _M_in_cur moved in the pback buffer.</span>
    <span class="hljs-variable">_M_pback_cur_save</span> += this-&gt;gptr() != this-&gt;eback();
    this-&gt;setg(<span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_pback_cur_save</span>, <span class="hljs-variable">_M_pback_end_save</span>);
    <span class="hljs-variable">_M_pback_init</span> = <span class="hljs-literal">false</span>;
  &#125;
    &#125;

  public:
    <span class="hljs-comment">// Constructors/destructor:</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Does not open any files.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  The default constructor initializes the parent class using its</span>
<span class="hljs-comment">     *  own default ctor.</span>
<span class="hljs-comment">     */</span>
    basic_filebuf();

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  The destructor closes the file first.</span>
<span class="hljs-comment">     */</span>
    virtual
    ~basic_filebuf()
    &#123; this-&gt;close(); &#125;

    <span class="hljs-comment">// Members:</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Returns true if the external file is open.</span>
<span class="hljs-comment">     */</span>
    bool
    is_open() const <span class="hljs-keyword">throw</span>()
    &#123; return <span class="hljs-variable">_M_file</span>.is_open(); &#125;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Opens an external file.</span>
<span class="hljs-comment">     *  @param  __s  The name of the file.</span>
<span class="hljs-comment">     *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">     *  @return  @c this on success, NULL on failure</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If a file is already open, this function immediately fails.</span>
<span class="hljs-comment">     *  Otherwise it tries to open the file named @a __s using the flags</span>
<span class="hljs-comment">     *  given in @a __mode.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  Table 92, adapted here, gives the relation between openmode</span>
<span class="hljs-comment">     *  combinations and the equivalent @c fopen() flags.</span>
<span class="hljs-comment">     *  (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,</span>
<span class="hljs-comment">     *  and binary|in|app per DR 596)</span>
<span class="hljs-comment">     *  &lt;pre&gt;</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  | ios_base Flag combination            stdio equivalent   |</span>
<span class="hljs-comment">     *  |binary  in  out  trunc  app                              |</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  |             +                        w                  |</span>
<span class="hljs-comment">     *  |             +           +            a                  |</span>
<span class="hljs-comment">     *  |                         +            a                  |</span>
<span class="hljs-comment">     *  |             +     +                  w                  |</span>
<span class="hljs-comment">     *  |         +                            r                  |</span>
<span class="hljs-comment">     *  |         +   +                        r+                 |</span>
<span class="hljs-comment">     *  |         +   +     +                  w+                 |</span>
<span class="hljs-comment">     *  |         +   +           +            a+                 |</span>
<span class="hljs-comment">     *  |         +               +            a+                 |</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  |   +         +                        wb                 |</span>
<span class="hljs-comment">     *  |   +         +           +            ab                 |</span>
<span class="hljs-comment">     *  |   +                     +            ab                 |</span>
<span class="hljs-comment">     *  |   +         +     +                  wb                 |</span>
<span class="hljs-comment">     *  |   +     +                            rb                 |</span>
<span class="hljs-comment">     *  |   +     +   +                        r+b                |</span>
<span class="hljs-comment">     *  |   +     +   +     +                  w+b                |</span>
<span class="hljs-comment">     *  |   +     +   +           +            a+b                |</span>
<span class="hljs-comment">     *  |   +     +               +            a+b                |</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  &lt;/pre&gt;</span>
<span class="hljs-comment">     */</span>
    <span class="hljs-variable">__filebuf_type</span>*
    open(const char* <span class="hljs-variable">__s</span>, ios_base::openmode <span class="hljs-variable">__mode</span>);

<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Opens an external file.</span>
<span class="hljs-comment">     *  @param  __s  The name of the file.</span>
<span class="hljs-comment">     *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">     *  @return  @c this on success, NULL on failure</span>
<span class="hljs-comment">     */</span>
    <span class="hljs-variable">__filebuf_type</span>*
    open(const std::string&amp; <span class="hljs-variable">__s</span>, ios_base::openmode <span class="hljs-variable">__mode</span>)
    &#123; return open(<span class="hljs-variable">__s</span>.c_str(), <span class="hljs-variable">__mode</span>); &#125;

<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">// C++11</span></span>

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Closes the currently associated file.</span>
<span class="hljs-comment">     *  @return  @c this on success, NULL on failure</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If no file is currently open, this function immediately fails.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If a &lt;em&gt;put buffer area&lt;/em&gt; exists, @c overflow(eof) is</span>
<span class="hljs-comment">     *  called to flush all the characters.  The file is then</span>
<span class="hljs-comment">     *  closed.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If any operations fail, this function also fails.</span>
<span class="hljs-comment">     */</span>
    <span class="hljs-variable">__filebuf_type</span>*
    close();

  protected:
    void
    <span class="hljs-variable">_M_allocate_internal_buffer</span>();

    void
    <span class="hljs-variable">_M_destroy_internal_buffer</span>() <span class="hljs-keyword">throw</span>();

    <span class="hljs-comment">// [27.8.1.4] overridden virtual functions</span>
    virtual streamsize
    showmanyc();

    <span class="hljs-comment">// Stroustrup, 1998, p. 628</span>
    <span class="hljs-comment">// underflow() and uflow() functions are called to get the next</span>
    <span class="hljs-comment">// character from the real input source when the buffer is empty.</span>
    <span class="hljs-comment">// Buffered input uses underflow()</span>

    virtual int_type
    underflow();

    virtual int_type
    pbackfail(int_type <span class="hljs-variable">__c</span> = <span class="hljs-variable">_Traits</span>::eof());

    <span class="hljs-comment">// Stroustrup, 1998, p 648</span>
    <span class="hljs-comment">// The overflow() function is called to transfer characters to the</span>
    <span class="hljs-comment">// real output destination when the buffer is full. A call to</span>
    <span class="hljs-comment">// overflow(c) outputs the contents of the buffer plus the</span>
    <span class="hljs-comment">// character c.</span>
    <span class="hljs-comment">// 27.5.2.4.5</span>
    <span class="hljs-comment">// Consume some sequence of the characters in the pending sequence.</span>
    virtual int_type
    overflow(int_type <span class="hljs-variable">__c</span> = <span class="hljs-variable">_Traits</span>::eof());

    <span class="hljs-comment">// Convert internal byte sequence to external, char-based</span>
    <span class="hljs-comment">// sequence via codecvt.</span>
    bool
    <span class="hljs-variable">_M_convert_to_external</span>(char_type*, streamsize);

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Manipulates the buffer.</span>
<span class="hljs-comment">     *  @param  __s  Pointer to a buffer area.</span>
<span class="hljs-comment">     *  @param  __n  Size of @a __s.</span>
<span class="hljs-comment">     *  @return  @c this</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If no file has been opened, and both @a __s and @a __n are zero, then</span>
<span class="hljs-comment">     *  the stream becomes unbuffered.  Otherwise, @c __s is used as a</span>
<span class="hljs-comment">     *  buffer; see</span>
<span class="hljs-comment">     *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering</span>
<span class="hljs-comment">     *  for more.</span>
<span class="hljs-comment">     */</span>
    virtual <span class="hljs-variable">__streambuf_type</span>*
    setbuf(char_type* <span class="hljs-variable">__s</span>, streamsize <span class="hljs-variable">__n</span>);

    virtual pos_type
    seekoff(off_type <span class="hljs-variable">__off</span>, ios_base::seekdir <span class="hljs-variable">__way</span>,
      ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out);

    virtual pos_type
    seekpos(pos_type <span class="hljs-variable">__pos</span>,
      ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out);

    <span class="hljs-comment">// Common code for seekoff, seekpos, and overflow</span>
    pos_type
    <span class="hljs-variable">_M_seek</span>(off_type <span class="hljs-variable">__off</span>, ios_base::seekdir <span class="hljs-variable">__way</span>, <span class="hljs-variable">__state_type</span> <span class="hljs-variable">__state</span>);

    int
    <span class="hljs-variable">_M_get_ext_pos</span>(<span class="hljs-variable">__state_type</span> &amp;<span class="hljs-variable">__state</span>);

    virtual int
    sync();

    virtual void
    imbue(const locale&amp; <span class="hljs-variable">__loc</span>);

    virtual streamsize
    xsgetn(char_type* <span class="hljs-variable">__s</span>, streamsize <span class="hljs-variable">__n</span>);

    virtual streamsize
    xsputn(const char_type* <span class="hljs-variable">__s</span>, streamsize <span class="hljs-variable">__n</span>);

    <span class="hljs-comment">// Flushes output buffer, then writes unshift sequence.</span>
    bool
    <span class="hljs-variable">_M_terminate_output</span>();

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  This function sets the pointers of the internal buffer, both get</span>
<span class="hljs-comment">     *  and put areas. Typically:</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *   __off == egptr() - eback() upon underflow/uflow (@b read mode);</span>
<span class="hljs-comment">     *   __off == 0 upon overflow (@b write mode);</span>
<span class="hljs-comment">     *   __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size</span>
<span class="hljs-comment">     *  reflects the actual allocated memory and the last cell is reserved</span>
<span class="hljs-comment">     *  for the overflow char of a full put area.</span>
<span class="hljs-comment">     */</span>
    void
    <span class="hljs-variable">_M_set_buffer</span>(streamsize <span class="hljs-variable">__off</span>)
    &#123;
const bool <span class="hljs-variable">__testin</span> = <span class="hljs-variable">_M_mode</span> &amp; ios_base::<span class="hljs-built_in">in</span>;
const bool <span class="hljs-variable">__testout</span> = (<span class="hljs-variable">_M_mode</span> &amp; ios_base::out
      || <span class="hljs-variable">_M_mode</span> &amp; ios_base::app);

<span class="hljs-keyword">if</span> (<span class="hljs-variable">__testin</span> &amp;&amp; <span class="hljs-variable">__off</span> &gt; <span class="hljs-number">0</span>)
  this-&gt;setg(<span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_buf</span> + <span class="hljs-variable">__off</span>);
<span class="hljs-keyword">else</span>
  this-&gt;setg(<span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_buf</span>);

<span class="hljs-keyword">if</span> (<span class="hljs-variable">__testout</span> &amp;&amp; <span class="hljs-variable">__off</span> == <span class="hljs-number">0</span> &amp;&amp; <span class="hljs-variable">_M_buf_size</span> &gt; <span class="hljs-number">1</span> )
  this-&gt;setp(<span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_buf</span> + <span class="hljs-variable">_M_buf_size</span> - <span class="hljs-number">1</span>);
<span class="hljs-keyword">else</span>
  this-&gt;setp(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
    &#125;
  &#125;;


  <span class="hljs-comment">//basic_fstream类,继承basic_iostream</span>
  <span class="hljs-comment">//basic_fstream类就是fstream类,只不过使用了typedef</span>
  <span class="hljs-comment">//typedef basic_fstream&lt;char&gt; fstream;</span>

  <span class="hljs-comment">// [27.8.1.11] Template class basic_fstream</span>
  <span class="hljs-comment">/**</span>
<span class="hljs-comment">   *  @brief  Controlling input and output for files.</span>
<span class="hljs-comment">   *  @ingroup io</span>
<span class="hljs-comment">   *</span>
<span class="hljs-comment">   *  @tparam _CharT  Type of character stream.</span>
<span class="hljs-comment">   *  @tparam _Traits  Traits for character type, defaults to</span>
<span class="hljs-comment">   *                   char_traits&lt;_CharT&gt;.</span>
<span class="hljs-comment">   *</span>
<span class="hljs-comment">   *  This class supports reading from and writing to named files, using</span>
<span class="hljs-comment">   *  the inherited functions from std::basic_iostream.  To control the</span>
<span class="hljs-comment">   *  associated sequence, an instance of std::basic_filebuf is used, which</span>
<span class="hljs-comment">   *  this page refers to as @c sb.</span>
<span class="hljs-comment">   */</span>
  template&lt;<span class="hljs-built_in">typename</span> <span class="hljs-variable">_CharT</span>, <span class="hljs-built_in">typename</span> <span class="hljs-variable">_Traits</span>&gt;
    class basic_fstream : public basic_iostream&lt;<span class="hljs-variable">_CharT</span>, <span class="hljs-variable">_Traits</span>&gt;
    &#123;
    public:
      <span class="hljs-comment">// Types:</span>
      typedef <span class="hljs-variable">_CharT</span> 					char_type;
      typedef <span class="hljs-variable">_Traits</span> 					traits_type;
      typedef <span class="hljs-built_in">typename</span> traits_type::int_type 		int_type;
      typedef <span class="hljs-built_in">typename</span> traits_type::pos_type 		pos_type;
      typedef <span class="hljs-built_in">typename</span> traits_type::off_type 		off_type;

      <span class="hljs-comment">// Non-standard types:</span>
      typedef basic_filebuf&lt;char_type, traits_type&gt; 	<span class="hljs-variable">__filebuf_type</span>;
      typedef basic_ios&lt;char_type, traits_type&gt;		<span class="hljs-variable">__ios_type</span>;
      typedef basic_iostream&lt;char_type, traits_type&gt;	<span class="hljs-variable">__iostream_type</span>;

    <span class="hljs-keyword">private</span>:
      <span class="hljs-variable">__filebuf_type</span>	<span class="hljs-variable">_M_filebuf</span>;

    public:
      <span class="hljs-comment">// Constructors/destructor:</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Default constructor.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Initializes @c sb using its default constructor, and passes</span>
<span class="hljs-comment">       *  @c &amp;sb to the base class initializer.  Does not open any files</span>
<span class="hljs-comment">       *  (you haven&#x27;t given it a filename to open).</span>
<span class="hljs-comment">       */</span>
      basic_fstream()
      : <span class="hljs-variable">__iostream_type</span>(), <span class="hljs-variable">_M_filebuf</span>()
      &#123; this-&gt;init(&amp;<span class="hljs-variable">_M_filebuf</span>); &#125;

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Create an input/output file stream.</span>
<span class="hljs-comment">       *  @param  __s  Null terminated string specifying the filename.</span>
<span class="hljs-comment">       *  @param  __mode  Open file in specified mode (see std::ios_base).</span>
<span class="hljs-comment">       */</span>
      explicit
      basic_fstream(const char* <span class="hljs-variable">__s</span>,
        ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      : <span class="hljs-variable">__iostream_type</span>(<span class="hljs-number">0</span>), <span class="hljs-variable">_M_filebuf</span>()
      &#123;
  this-&gt;init(&amp;<span class="hljs-variable">_M_filebuf</span>);
  this-&gt;open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>);
      &#125;

<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Create an input/output file stream.</span>
<span class="hljs-comment">       *  @param  __s  Null terminated string specifying the filename.</span>
<span class="hljs-comment">       *  @param  __mode  Open file in specified mode (see std::ios_base).</span>
<span class="hljs-comment">       */</span>
      explicit
      basic_fstream(const std::string&amp; <span class="hljs-variable">__s</span>,
        ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      : <span class="hljs-variable">__iostream_type</span>(<span class="hljs-number">0</span>), <span class="hljs-variable">_M_filebuf</span>()
      &#123;
  this-&gt;init(&amp;<span class="hljs-variable">_M_filebuf</span>);
  this-&gt;open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>);
      &#125;

      basic_fstream(const basic_fstream&amp;) = delete;

      basic_fstream(basic_fstream&amp;&amp; <span class="hljs-variable">__rhs</span>)
      : <span class="hljs-variable">__iostream_type</span>(std::<span class="hljs-built_in">move</span>(<span class="hljs-variable">__rhs</span>)),
      <span class="hljs-variable">_M_filebuf</span>(std::<span class="hljs-built_in">move</span>(<span class="hljs-variable">__rhs</span>.<span class="hljs-variable">_M_filebuf</span>))
      &#123; <span class="hljs-variable">__iostream_type</span>::set_rdbuf(&amp;<span class="hljs-variable">_M_filebuf</span>); &#125;
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  The destructor does nothing.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  The file is closed by the filebuf object, not the formatting</span>
<span class="hljs-comment">       *  stream.</span>
<span class="hljs-comment">       */</span>
      ~basic_fstream()
      &#123; &#125;

      <span class="hljs-comment">// Members:</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Accessing the underlying buffer.</span>
<span class="hljs-comment">       *  @return  The current basic_filebuf buffer.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  This hides both signatures of std::basic_ios::rdbuf().</span>
<span class="hljs-comment">       */</span>
      <span class="hljs-variable">__filebuf_type</span>*
      rdbuf() const
      &#123; return const_cast&lt;<span class="hljs-variable">__filebuf_type</span>*&gt;(&amp;<span class="hljs-variable">_M_filebuf</span>); &#125;

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Wrapper to test for an open file.</span>
<span class="hljs-comment">       *  @return  @c rdbuf()-&gt;is_open()</span>
<span class="hljs-comment">       */</span>
      bool
      is_open()
      &#123; return <span class="hljs-variable">_M_filebuf</span>.is_open(); &#125;

      <span class="hljs-comment">// _GLIBCXX_RESOLVE_LIB_DEFECTS</span>
      <span class="hljs-comment">// 365. Lack of const-qualification in clause 27</span>
      bool
      is_open() const
      &#123; return <span class="hljs-variable">_M_filebuf</span>.is_open(); &#125;

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Opens an external file.</span>
<span class="hljs-comment">       *  @param  __s  The name of the file.</span>
<span class="hljs-comment">       *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Calls @c std::basic_filebuf::open(__s,__mode).  If that</span>
<span class="hljs-comment">       *  function fails, @c failbit is set in the stream&#x27;s error state.</span>
<span class="hljs-comment">       */</span>
      void
      open(const char* <span class="hljs-variable">__s</span>,
     ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      &#123;
  <span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_filebuf</span>.open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>))
    this-&gt;setstate(ios_base::failbit);
  <span class="hljs-keyword">else</span>
    <span class="hljs-comment">// _GLIBCXX_RESOLVE_LIB_DEFECTS</span>
    <span class="hljs-comment">// 409. Closing an fstream should clear error state</span>
    this-&gt;clear();
      &#125;

<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Opens an external file.</span>
<span class="hljs-comment">       *  @param  __s  The name of the file.</span>
<span class="hljs-comment">       *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Calls @c std::basic_filebuf::open(__s,__mode).  If that</span>
<span class="hljs-comment">       *  function fails, @c failbit is set in the stream&#x27;s error state.</span>
<span class="hljs-comment">       */</span>
      void
      open(const std::string&amp; <span class="hljs-variable">__s</span>,
     ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      &#123;
  <span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_filebuf</span>.open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>))
    this-&gt;setstate(ios_base::failbit);
  <span class="hljs-keyword">else</span>
    <span class="hljs-comment">// _GLIBCXX_RESOLVE_LIB_DEFECTS</span>
    <span class="hljs-comment">// 409. Closing an fstream should clear error state</span>
    this-&gt;clear();
      &#125;

<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">// C++11</span></span>

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Close the file.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Calls @c std::basic_filebuf::close().  If that function</span>
<span class="hljs-comment">       *  fails, @c failbit is set in the stream&#x27;s error state.</span>
<span class="hljs-comment">       */</span>
      void
      close()
      &#123;
  <span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_filebuf</span>.close())
    this-&gt;setstate(ios_base::failbit);
      &#125;
    &#125;;
</code></pre>

<p>&emsp;&emsp;看的头皮发麻,还得删掉一些,得找到重点</p>
<pre><code class="hljs sqf"><span class="hljs-comment">/** @file include/fstream</span>
<span class="hljs-comment"> *  This is a Standard C++ Library header.</span>
<span class="hljs-comment"> */</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;istream&gt;             //输入流头文件</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;ostream&gt;             //输出流头文件</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/codecvt.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;             // For BUFSIZ</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/basic_file.h&gt;  // For __basic_file, __c_lock</span></span>
<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;             // For std::string overloads.</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>


<span class="hljs-comment">//basic_filebuf类,继承的basic_streambuf类</span>
template&lt;<span class="hljs-built_in">typename</span> <span class="hljs-variable">_CharT</span>, <span class="hljs-built_in">typename</span> <span class="hljs-variable">_Traits</span>&gt;
  class basic_filebuf : public basic_streambuf&lt;<span class="hljs-variable">_CharT</span>, <span class="hljs-variable">_Traits</span>&gt;
  &#123;
  public:
    typedef <span class="hljs-variable">_CharT</span>                     	      char_type;<span class="hljs-comment">//重命名_CharT为char_type</span>
    typedef <span class="hljs-variable">_Traits</span>                    	      traits_type;<span class="hljs-comment">//重命名_Traits为traits_type</span>
    typedef <span class="hljs-built_in">typename</span> traits_type::int_type 		int_type;<span class="hljs-comment">//同理</span>
    typedef <span class="hljs-built_in">typename</span> traits_type::pos_type 		pos_type;<span class="hljs-comment">//同理</span>
    typedef <span class="hljs-built_in">typename</span> traits_type::off_type 		off_type;<span class="hljs-comment">//同理</span>

    typedef basic_streambuf&lt;char_type, traits_type&gt;  	<span class="hljs-variable">__streambuf_type</span>;<span class="hljs-comment">//同理</span>

    <span class="hljs-comment">//!!!!!!!!!!!!!</span>
    <span class="hljs-comment">//留意一下,比较重要,它将自身重命名了__filebuf_type类型,也就是说__filebuf_type类型的对象就是basic_filebuf类的对象</span>
    typedef basic_filebuf&lt;char_type, traits_type&gt;     <span class="hljs-variable">__filebuf_type</span>;<span class="hljs-comment">//重命名basic_filebuf&lt;char_type, traits_type&gt;为__filebuf_type</span>


    typedef <span class="hljs-variable">__basic_file</span>&lt;char&gt;		        <span class="hljs-variable">__file_type</span>;<span class="hljs-comment">//同理</span>
    typedef <span class="hljs-built_in">typename</span> traits_type::state_type          <span class="hljs-variable">__state_type</span>;<span class="hljs-comment">//同理</span>
    typedef codecvt&lt;char_type, char, <span class="hljs-variable">__state_type</span>&gt;    <span class="hljs-variable">__codecvt_type</span>;<span class="hljs-comment">//同理</span>

    <span class="hljs-comment">//朋友类,ios_base,也就ios::in,ios::out这些</span>
    friend class ios_base; <span class="hljs-comment">// For sync_with_stdio.</span>

  protected:
    <span class="hljs-comment">//数据定义</span>
    <span class="hljs-variable">__file_type</span> 		<span class="hljs-variable">_M_file</span>;<span class="hljs-comment">//留意一下,自身保存了自己的一个对象!</span>
    ios_base::openmode 	<span class="hljs-variable">_M_mode</span>;    <span class="hljs-comment">//打开文件的模式</span>

    <span class="hljs-variable">__c_lock</span>          	<span class="hljs-variable">_M_lock</span>;
    <span class="hljs-variable">__state_type</span> 		<span class="hljs-variable">_M_state_beg</span>;<span class="hljs-comment">//文件指针状态开头</span>
    <span class="hljs-variable">__state_type</span>		<span class="hljs-variable">_M_state_cur</span>;<span class="hljs-comment">//文件指针状态当前位置</span>
    <span class="hljs-variable">__state_type</span>		<span class="hljs-variable">_M_state_last</span>;<span class="hljs-comment">//文件指针状态末尾</span>
    <span class="hljs-comment">/// Pointer to the beginning of internal buffer.</span>
    char_type*		<span class="hljs-variable">_M_buf</span>;
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Actual size of internal buffer. This number is equal to the size</span>
<span class="hljs-comment">     *  of the put area + 1 position, reserved for the overflow char of</span>
<span class="hljs-comment">     *  a full area.</span>
<span class="hljs-comment">     */</span>
    size_t			<span class="hljs-variable">_M_buf_size</span>;
    <span class="hljs-comment">// Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.</span>
    bool			<span class="hljs-variable">_M_buf_allocated</span>;
    bool                      <span class="hljs-variable">_M_reading</span>;
    bool                      <span class="hljs-variable">_M_writing</span>;
    char_type			<span class="hljs-variable">_M_pback</span>;
    char_type*		<span class="hljs-variable">_M_pback_cur_save</span>;
    char_type*		<span class="hljs-variable">_M_pback_end_save</span>;
    bool			<span class="hljs-variable">_M_pback_init</span>;
    const <span class="hljs-variable">__codecvt_type</span>* 	<span class="hljs-variable">_M_codecvt</span>;
    char*			<span class="hljs-variable">_M_ext_buf</span>;
    streamsize		<span class="hljs-variable">_M_ext_buf_size</span>;
    const char*		<span class="hljs-variable">_M_ext_next</span>;
    char*			<span class="hljs-variable">_M_ext_end</span>;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Initializes pback buffers, and moves normal buffers to safety.</span>
<span class="hljs-comment">     *  Assumptions:</span>
<span class="hljs-comment">     *  _M_in_cur has already been moved back</span>
<span class="hljs-comment">     */</span>
    void
    <span class="hljs-variable">_M_create_pback</span>()
    &#123;
<span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_pback_init</span>)
  &#123;
    <span class="hljs-variable">_M_pback_cur_save</span> = this-&gt;gptr();
    <span class="hljs-variable">_M_pback_end_save</span> = this-&gt;egptr();
    this-&gt;setg(&amp;<span class="hljs-variable">_M_pback</span>, &amp;<span class="hljs-variable">_M_pback</span>, &amp;<span class="hljs-variable">_M_pback</span> + <span class="hljs-number">1</span>);
    <span class="hljs-variable">_M_pback_init</span> = <span class="hljs-literal">true</span>;
  &#125;
    &#125;

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  Deactivates pback buffer contents, and restores normal buffer.</span>
<span class="hljs-comment">     *  Assumptions:</span>
<span class="hljs-comment">     *  The pback buffer has only moved forward.</span>
<span class="hljs-comment">     */</span>
    void
    <span class="hljs-variable">_M_destroy_pback</span>() <span class="hljs-keyword">throw</span>()
    &#123;
<span class="hljs-keyword">if</span> (<span class="hljs-variable">_M_pback_init</span>)
  &#123;
    <span class="hljs-comment">// Length _M_in_cur moved in the pback buffer.</span>
    <span class="hljs-variable">_M_pback_cur_save</span> += this-&gt;gptr() != this-&gt;eback();
    this-&gt;setg(<span class="hljs-variable">_M_buf</span>, <span class="hljs-variable">_M_pback_cur_save</span>, <span class="hljs-variable">_M_pback_end_save</span>);
    <span class="hljs-variable">_M_pback_init</span> = <span class="hljs-literal">false</span>;
  &#125;
    &#125;

  public:

    <span class="hljs-comment">// Constructors/destructor:</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Does not open any files.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  The default constructor initializes the parent class using its</span>
<span class="hljs-comment">     *  own default ctor.</span>
<span class="hljs-comment">     */</span>
    basic_filebuf();<span class="hljs-comment">//构造函数</span>

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  The destructor closes the file first.</span>
<span class="hljs-comment">     */</span>
    virtual
    ~basic_filebuf()<span class="hljs-comment">//销毁函数</span>
    &#123; this-&gt;close(); &#125;

    <span class="hljs-comment">// Members:</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Returns true if the external file is open.</span>
<span class="hljs-comment">     */</span>
    bool
    is_open() const <span class="hljs-keyword">throw</span>()<span class="hljs-comment">//判断是否打开文件</span>
    &#123; return <span class="hljs-variable">_M_file</span>.is_open(); &#125;

    <span class="hljs-comment">//我们的重点,打开文件的函数</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Opens an external file.</span>
<span class="hljs-comment">     *  @param  __s  The name of the file.</span>
<span class="hljs-comment">     *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">     *  @return  @c this on success, NULL on failure</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If a file is already open, this function immediately fails.</span>
<span class="hljs-comment">     *  Otherwise it tries to open the file named @a __s using the flags</span>
<span class="hljs-comment">     *  given in @a __mode.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  Table 92, adapted here, gives the relation between openmode</span>
<span class="hljs-comment">     *  combinations and the equivalent @c fopen() flags.</span>
<span class="hljs-comment">     *  (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,</span>
<span class="hljs-comment">     *  and binary|in|app per DR 596)</span>
<span class="hljs-comment">     *  &lt;pre&gt;</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  | ios_base Flag combination            stdio equivalent   |</span>
<span class="hljs-comment">     *  |binary  in  out  trunc  app                              |</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  |             +                        w                  |</span>
<span class="hljs-comment">     *  |             +           +            a                  |</span>
<span class="hljs-comment">     *  |                         +            a                  |</span>
<span class="hljs-comment">     *  |             +     +                  w                  |</span>
<span class="hljs-comment">     *  |         +                            r                  |</span>
<span class="hljs-comment">     *  |         +   +                        r+                 |</span>
<span class="hljs-comment">     *  |         +   +     +                  w+                 |</span>
<span class="hljs-comment">     *  |         +   +           +            a+                 |</span>
<span class="hljs-comment">     *  |         +               +            a+                 |</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  |   +         +                        wb                 |</span>
<span class="hljs-comment">     *  |   +         +           +            ab                 |</span>
<span class="hljs-comment">     *  |   +                     +            ab                 |</span>
<span class="hljs-comment">     *  |   +         +     +                  wb                 |</span>
<span class="hljs-comment">     *  |   +     +                            rb                 |</span>
<span class="hljs-comment">     *  |   +     +   +                        r+b                |</span>
<span class="hljs-comment">     *  |   +     +   +     +                  w+b                |</span>
<span class="hljs-comment">     *  |   +     +   +           +            a+b                |</span>
<span class="hljs-comment">     *  |   +     +               +            a+b                |</span>
<span class="hljs-comment">     *  +---------------------------------------------------------+</span>
<span class="hljs-comment">     *  &lt;/pre&gt;</span>
<span class="hljs-comment">     */</span>
     <span class="hljs-comment">//没有函数体,视乎是一个声明,不管了,先放着一边,返回值是自身类型的一个指针</span>
    <span class="hljs-variable">__filebuf_type</span>*
    open(const char* <span class="hljs-variable">__s</span>, ios_base::openmode <span class="hljs-variable">__mode</span>);

<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Opens an external file.</span>
<span class="hljs-comment">     *  @param  __s  The name of the file.</span>
<span class="hljs-comment">     *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">     *  @return  @c this on success, NULL on failure</span>
<span class="hljs-comment">     */</span>
     <span class="hljs-comment">//又是一个打开文件函数,不管使用了预编译,需要大于特定版本</span>
    <span class="hljs-variable">__filebuf_type</span>*
    open(const std::string&amp; <span class="hljs-variable">__s</span>, ios_base::openmode <span class="hljs-variable">__mode</span>)
    &#123; return open(<span class="hljs-variable">__s</span>.c_str(), <span class="hljs-variable">__mode</span>); &#125;

<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">// C++11</span></span>

    <span class="hljs-comment">/**</span>
<span class="hljs-comment">     *  @brief  Closes the currently associated file.</span>
<span class="hljs-comment">     *  @return  @c this on success, NULL on failure</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If no file is currently open, this function immediately fails.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If a &lt;em&gt;put buffer area&lt;/em&gt; exists, @c overflow(eof) is</span>
<span class="hljs-comment">     *  called to flush all the characters.  The file is then</span>
<span class="hljs-comment">     *  closed.</span>
<span class="hljs-comment">     *</span>
<span class="hljs-comment">     *  If any operations fail, this function also fails.</span>
<span class="hljs-comment">     */</span>
    <span class="hljs-variable">__filebuf_type</span>*
    close();<span class="hljs-comment">//关闭函数</span>

  protected:
    <span class="hljs-comment">//...还有一大把函数</span>
  &#125;;


  <span class="hljs-comment">//basic_fstream类,继承basic_iostream</span>
  <span class="hljs-comment">//basic_fstream类就是fstream类,只不过使用了typedef</span>
  <span class="hljs-comment">//typedef basic_fstream&lt;char&gt; fstream;</span>

  template&lt;<span class="hljs-built_in">typename</span> <span class="hljs-variable">_CharT</span>, <span class="hljs-built_in">typename</span> <span class="hljs-variable">_Traits</span>&gt;
    class basic_fstream : public basic_iostream&lt;<span class="hljs-variable">_CharT</span>, <span class="hljs-variable">_Traits</span>&gt;
    &#123;
    public:
      typedef <span class="hljs-variable">_CharT</span> 					char_type;<span class="hljs-comment">//重命名_CharT为char_type</span>
      typedef <span class="hljs-variable">_Traits</span> 					traits_type;<span class="hljs-comment">//重命名_Traits为traits_type</span>
      typedef <span class="hljs-built_in">typename</span> traits_type::int_type 		int_type;<span class="hljs-comment">//同理</span>
      typedef <span class="hljs-built_in">typename</span> traits_type::pos_type 		pos_type;<span class="hljs-comment">//同理</span>
      typedef <span class="hljs-built_in">typename</span> traits_type::off_type 		off_type;<span class="hljs-comment">//同理</span>

      <span class="hljs-comment">//留意一下,比较重要,它重命名了basic_filebuf为__filebuf_type</span>
      typedef basic_filebuf&lt;char_type, traits_type&gt; 	<span class="hljs-variable">__filebuf_type</span>;

      typedef basic_ios&lt;char_type, traits_type&gt;		<span class="hljs-variable">__ios_type</span>;<span class="hljs-comment">//同理</span>

      typedef basic_iostream&lt;char_type, traits_type&gt;	<span class="hljs-variable">__iostream_type</span>;<span class="hljs-comment">//同理</span>

    <span class="hljs-keyword">private</span>:
      <span class="hljs-comment">//只有一个私有对象,那就是上面提到的basic_filebuf类的对象</span>
      <span class="hljs-comment">//(重命名了basic_filebuf&lt;char_type, traits_type&gt;为basic_filebuf)</span>
      <span class="hljs-variable">__filebuf_type</span>	<span class="hljs-variable">_M_filebuf</span>;

    public:
      basic_fstream()<span class="hljs-comment">//构造函数</span>
      : <span class="hljs-variable">__iostream_type</span>(), <span class="hljs-variable">_M_filebuf</span>()
      &#123; this-&gt;init(&amp;<span class="hljs-variable">_M_filebuf</span>); &#125;

      explicit
      basic_fstream(const char* <span class="hljs-variable">__s</span>,
        ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)<span class="hljs-comment">//又是一个构造函数</span>
      : <span class="hljs-variable">__iostream_type</span>(<span class="hljs-number">0</span>), <span class="hljs-variable">_M_filebuf</span>()
      &#123;
  this-&gt;init(&amp;<span class="hljs-variable">_M_filebuf</span>);
  this-&gt;open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>);<span class="hljs-comment">//这个构造函数,调用了自身的一个打开函数,需要关注一下</span>
      &#125;

<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Create an input/output file stream.</span>
<span class="hljs-comment">       *  @param  __s  Null terminated string specifying the filename.</span>
<span class="hljs-comment">       *  @param  __mode  Open file in specified mode (see std::ios_base).</span>
<span class="hljs-comment">       */</span>
       <span class="hljs-comment">//预编译的构造函数,需要C++版本大于一定的版本</span>
      explicit
      basic_fstream(const std::string&amp; <span class="hljs-variable">__s</span>,
        ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      : <span class="hljs-variable">__iostream_type</span>(<span class="hljs-number">0</span>), <span class="hljs-variable">_M_filebuf</span>()
      &#123;
  this-&gt;init(&amp;<span class="hljs-variable">_M_filebuf</span>);
  this-&gt;open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>);<span class="hljs-comment">//也打开了这个函数</span>
      &#125;

      basic_fstream(const basic_fstream&amp;) = delete;

      basic_fstream(basic_fstream&amp;&amp; <span class="hljs-variable">__rhs</span>)
      : <span class="hljs-variable">__iostream_type</span>(std::<span class="hljs-built_in">move</span>(<span class="hljs-variable">__rhs</span>)),
      <span class="hljs-variable">_M_filebuf</span>(std::<span class="hljs-built_in">move</span>(<span class="hljs-variable">__rhs</span>.<span class="hljs-variable">_M_filebuf</span>))
      &#123; <span class="hljs-variable">__iostream_type</span>::set_rdbuf(&amp;<span class="hljs-variable">_M_filebuf</span>); &#125;
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  The destructor does nothing.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  The file is closed by the filebuf object, not the formatting</span>
<span class="hljs-comment">       *  stream.</span>
<span class="hljs-comment">       */</span>
      ~basic_fstream()<span class="hljs-comment">//析构函数</span>
      &#123; &#125;

      <span class="hljs-comment">// Members:</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Accessing the underlying buffer.</span>
<span class="hljs-comment">       *  @return  The current basic_filebuf buffer.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  This hides both signatures of std::basic_ios::rdbuf().</span>
<span class="hljs-comment">       */</span>
      <span class="hljs-variable">__filebuf_type</span>*
      rdbuf() const
      &#123; return const_cast&lt;<span class="hljs-variable">__filebuf_type</span>*&gt;(&amp;<span class="hljs-variable">_M_filebuf</span>); &#125;

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Wrapper to test for an open file.</span>
<span class="hljs-comment">       *  @return  @c rdbuf()-&gt;is_open()</span>
<span class="hljs-comment">       */</span>
      bool
      is_open()
      &#123; return <span class="hljs-variable">_M_filebuf</span>.is_open(); &#125;

      <span class="hljs-comment">// _GLIBCXX_RESOLVE_LIB_DEFECTS</span>
      <span class="hljs-comment">// 365. Lack of const-qualification in clause 27</span>
      bool
      is_open() const
      &#123; return <span class="hljs-variable">_M_filebuf</span>.is_open(); &#125;

<span class="hljs-comment">/*************************************这次解决问题的重点**********************/</span>

      <span class="hljs-comment">//在构造时,调用到的打开函数</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Opens an external file.</span>
<span class="hljs-comment">       *  @param  __s  The name of the file.</span>
<span class="hljs-comment">       *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Calls @c std::basic_filebuf::open(__s,__mode).  If that</span>
<span class="hljs-comment">       *  function fails, @c failbit is set in the stream&#x27;s error state.</span>
<span class="hljs-comment">       */</span>
       <span class="hljs-comment">//第一个参数是字符数组指针(字符串),第二个参数是打开模式</span>
       <span class="hljs-comment">//打开模式默认为ios_base::in | ios_base::out</span>
      void
      open(const char* <span class="hljs-variable">__s</span>,
     ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      &#123;
  <span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_filebuf</span>.open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>))<span class="hljs-comment">//调用basic_filebuf类中的open,如果打开失败,则设置failbit(会使得fail()函数返回真,其它的为false)</span>
    this-&gt;setstate(ios_base::failbit);
  <span class="hljs-keyword">else</span>
    <span class="hljs-comment">// _GLIBCXX_RESOLVE_LIB_DEFECTS</span>
    <span class="hljs-comment">// 409. Closing an fstream should clear error state</span>
    this-&gt;clear();<span class="hljs-comment">//打开成功的话,就清除错误状态,(可能之前就有)</span>
      &#125;

<span class="hljs-comment">/*************************************这次解决问题的重点**********************/</span>

<span class="hljs-meta">#if __cplusplus &gt;= 201103L</span>
      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Opens an external file.</span>
<span class="hljs-comment">       *  @param  __s  The name of the file.</span>
<span class="hljs-comment">       *  @param  __mode  The open mode flags.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Calls @c std::basic_filebuf::open(__s,__mode).  If that</span>
<span class="hljs-comment">       *  function fails, @c failbit is set in the stream&#x27;s error state.</span>
<span class="hljs-comment">       */</span>
      void
      open(const std::string&amp; <span class="hljs-variable">__s</span>,
     ios_base::openmode <span class="hljs-variable">__mode</span> = ios_base::<span class="hljs-built_in">in</span> | ios_base::out)
      &#123;
  <span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_filebuf</span>.open(<span class="hljs-variable">__s</span>, <span class="hljs-variable">__mode</span>))
    this-&gt;setstate(ios_base::failbit);
  <span class="hljs-keyword">else</span>
    <span class="hljs-comment">// _GLIBCXX_RESOLVE_LIB_DEFECTS</span>
    <span class="hljs-comment">// 409. Closing an fstream should clear error state</span>
    this-&gt;clear();
      &#125;

<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">// C++11</span></span>

      <span class="hljs-comment">/**</span>
<span class="hljs-comment">       *  @brief  Close the file.</span>
<span class="hljs-comment">       *</span>
<span class="hljs-comment">       *  Calls @c std::basic_filebuf::close().  If that function</span>
<span class="hljs-comment">       *  fails, @c failbit is set in the stream&#x27;s error state.</span>
<span class="hljs-comment">       */</span>
      void
      close()
      &#123;
  <span class="hljs-keyword">if</span> (!<span class="hljs-variable">_M_filebuf</span>.close())
    this-&gt;setstate(ios_base::failbit);
      &#125;
    &#125;;</code></pre>

<p>&emsp;&emsp;好家伙,总算找到了,它调用basic_filebuf类中的open函数,但是basic_filebuf类中的open函数只有一个声明,没有函数体,emmmmm,不管咯,先上网查一下basic_filebuf类.</p>
<p><a target="_blank" rel="noopener" href="https://www.apiref.com/cpp-zh/cpp/io/basic_filebuf.html">basic_filebuf类详情链接</a></p>
<p><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/basic_filebuf%E7%B1%BB1.png" srcset="/img/loading.gif" alt="basic_filebuf类"><br><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/basic_filebuf%E7%B1%BB2.png" srcset="/img/loading.gif" alt="basic_filebuf类"></p>
<p>&emsp;&emsp;点击open函数,看看是怎么样的</p>
<p><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/basic_filebuf%E7%B1%BB3open%E5%87%BD%E6%95%B0.png" srcset="/img/loading.gif" alt="basic_filebuf类的open函数"></p>
<p>&emsp;&emsp;!!!找到原因</p>
<p><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/basic_filebuf%E7%B1%BB3open%E5%87%BD%E6%95%B0%E5%B8%A6%E6%B3%A8%E8%A7%A3.png" srcset="/img/loading.gif" alt="basic_filebuf类的open函数"></p>
<blockquote>
<p>问题结果答案</p>
</blockquote>
<p>&emsp;&emsp;当我们在fstream类中使用ios::in|ios::out或者ios::in|ios::out打开文件时,就是r+(C语言文件打开方式,扩展性读取,可读可写,但是文件不存在打开失败!)或者r+b(C语言文件打开方式,扩展性读取二进制文件,可读可写,但是文件不存在打开失败!)</p>
<p><img src="https://cdn.jsdelivr.net/gh/Teimlz/teimlz.github.io@master/image/docs/%E5%85%B3%E4%BA%8EC-%E4%B8%ADfstream%E4%BD%BF%E7%94%A8-ios-in%E6%88%96ios-out-%E5%9C%A8%E5%BD%93%E6%96%87%E4%BB%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E4%B8%8D%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E8%80%8C%E6%98%AF%E6%89%93%E5%BC%80%E9%94%99%E8%AF%AF/result.png" srcset="/img/loading.gif" alt="问题答案结果"></p>
<blockquote>
<p>结语</p>
</blockquote>
<p>&emsp;&emsp;嗯,不错,总算解决了当初为什么一定要加ios::app才能创建文件的问题了.不过,我也感觉到自己好蠢啊,我丢,明明在basic_filebuf类中的open有个表的,自己也看了一下,结果就秒过去了,自己找basic_filebuf类的open函数的函数体找了半天,如果看到那个表,就能早早结束了……….</p>
<pre><code class="hljs gherkin"><span class="hljs-symbol">*</span>  Table 92, adapted here, gives the relation between openmode
<span class="hljs-symbol">*</span>  combinations and the equivalent <span class="hljs-meta">@c</span> fopen() flags.
<span class="hljs-symbol">*</span>  (NB: lines app, in|<span class="hljs-string">out</span>|<span class="hljs-string">app, in</span>|<span class="hljs-string">app, binary</span>|<span class="hljs-string">app, binary</span>|<span class="hljs-string">in</span>|<span class="hljs-string">out</span>|<span class="hljs-string">app,</span>
<span class="hljs-string">*  and binary</span>|<span class="hljs-string">in</span>|<span class="hljs-string">app per DR 596)</span>
<span class="hljs-string">*  &lt;pre&gt;</span>
<span class="hljs-string">*  +---------------------------------------------------------+</span>
<span class="hljs-string">*  </span>|<span class="hljs-string"> ios_base Flag combination            stdio equivalent   </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">binary  in  out  trunc  app                              </span>|
<span class="hljs-symbol">*</span>  +---------------------------------------------------------+
<span class="hljs-symbol">*</span>  |<span class="hljs-string">             +                        w                  </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">             +           +            a                  </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">                         +            a                  </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">             +     +                  w                  </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">         +                            r                  </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">         +   +                        r+                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">         +   +     +                  w+                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">         +   +           +            a+                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">         +               +            a+                 </span>|
<span class="hljs-symbol">*</span>  +---------------------------------------------------------+
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +         +                        wb                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +         +           +            ab                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +                     +            ab                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +         +     +                  wb                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +     +                            rb                 </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +     +   +                        r+b                </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +     +   +     +                  w+b                </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +     +   +           +            a+b                </span>|
<span class="hljs-symbol">*</span>  |<span class="hljs-string">   +     +               +            a+b                </span>|
<span class="hljs-symbol">*</span>  +---------------------------------------------------------+</code></pre>
<p>&emsp;&emsp; 现在时间是晚上2点14分,该睡觉了,晚安.</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/C/">C++</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%BC%96%E7%A8%8B/">编程</a>
                    
                      <a class="hover-with-bg" href="/tags/C/">C++</a>
                    
                      <a class="hover-with-bg" href="/tags/%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C/">文件操作</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/06/27/%E4%BD%BF%E7%94%A8WSL%E7%9A%84%E5%8A%A0%E5%BF%AB%E4%BD%A0%E7%9A%84%E4%B8%8B%E8%BD%BD/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">使用WSL的加快你的下载</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/11/21/%E6%90%AD%E5%BB%BA%E4%B8%80%E4%BB%BD%E5%B1%9E%E4%BA%8E%E4%BD%A0%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%90%A7/">
                        <span class="hidden-mobile">搭建一份属于你的博客吧</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    Fluid.utils.waitElementVisible('vcomments', function() {
      Fluid.utils.createScript('https://cdn.staticfile.org/valine/1.4.14/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "ca7LySCTd27urkDy9amsfLEe-gzGzoHsz",
          app_key: "5yeLnkYSsoGkC95nsm0gs5F3",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the
    <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments powered by Valine.</a>
  </noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
    <!-- cnzz Analytics Icon -->
    <span id="cnzz_stat_icon_1279445150" style="display: none"></span>
  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":200})
    NProgress.start()
    document.addEventListener('DOMContentLoaded', function() {
      window.NProgress && window.NProgress.inc();
    })
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.5.3/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://cdn.staticfile.org/tocbot/4.12.0/tocbot.min.js" ></script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>





  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>












  
    <!-- Baidu Analytics -->
    <script defer>
      var _hmt = _hmt || [];
      (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?6c1436d3df29ec7c0cda293342e60e93";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
  

  

  

  

  

  
    <!-- cnzz Analytics -->
    <script defer src="//s4.cnzz.com/z_stat.php?id=1279445150&show=pic"
            type="text/javascript"></script>
  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
